menuList[2].onclick();

async function fun(){
    let one = await getSortOne();
    // let two = await getSortTwo(one[0].bigId);
    // let three = await getSortThreee(two[0].mediumId);
    
}
fun();

    // 得到一级分类 并渲染
function getSortOne(){
    let sortUlOne = cl("sortUlOne"); //一级容器
    sortUlOne.innerHTML = "";
    return new Promise((resolve, reject) => {
        axios({
            url:'/admin/goods/findFirst',
            method:'GET',
            })
            .then(data => {
                if(data.data.code==20000){
                    let i=0;
                    for(let key of data.data.data){
                        sortUlOne.innerHTML+=`
                        <li class="sortOneLi" _id="${i}" name="${key.bigId}" flags="1" onclick="sortOneFun()">${key.btype}</li>
                        `
                        i++;
                    }
                }
                console.log(data.data.code==20000);
                resolve(data.data.data);
            })
            .catch(err => {
                console.log(err);
            })
    });
   
}
// 得到二级分类(bigId) 并渲染
function getSortTwo(bigId){
    let sortUlTwo = cl("sortUlTwo"); //二级容器
    sortUlTwo.innerHTML = "";
    return new Promise((resolve, reject) => {
        axios({
            url:'/admin/goods/findSecond',
            method:'GET',
            params: {
                bigId:bigId
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    console.log(data.data.data.length);
                    if(data.data.data.length==0){
                        sortUlTwo.innerHTML ="该分类有待添加"
                    }else{
                        let i=0
                        for(let key of data.data.data){
                            sortUlTwo.innerHTML+=`
                            <li class="sortTwoLi" _id="${i}" name="${key.mediumId}" bigId="${bigId}" flags="2" onclick="sortTwoFun()">${key.mtype}</li>
                            `
                            i++
                        }
                    }
                }
                console.log(data.data.code==20000);
                resolve(data.data.data);
            })
            .catch(err => {
                console.log(err);
            })
    });
   
}
// 得到三级分类(mediumId)  并渲染
function getSortThree(mediumId){
    let sortUlThree = cl("sortUlThree"); //三级容器
    console.log(sortUlThree);
    sortUlThree.innerHTML = "";
    return new Promise((resolve, reject) => {
        axios({
            url:'/admin/goods/findThird',
            method:'GET',
            params: {
                mediumId:mediumId
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    if(data.data.data.length==0){
                        sortUlThree.innerHTML = "该分类有待添加"
                    }else{
                        let i=0;
                        for(let key of data.data.data){
                            sortUlThree.innerHTML+=`
                            <li class="sortThreeLi" _id="${i}" name="${key.littleId}" mediumId="${mediumId}" flags="3" onclick="sortThreeFun()">${key.ltype}</li>
                            `
                            i++
                        }
                       
                    }
                }
                resolve(data.data.data);
            })
            .catch(err => {
                console.log(err);
            })
    });
}
// 得到四级分类(littleId)  并渲染
function getSortFour(littleId){
    let sortUlFour = cl("sortUlFour"); //四级容器
    console.log(sortUlFour);
    sortUlFour.innerHTML = "";
    return new Promise((resolve, reject) => {
        axios({
            url:'/admin/goods/findFour',
            method:'GET',
            params: {
                littleId:littleId
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    if(data.data.data.length==0){
                        sortUlFour.innerHTML = "该分类有待添加"
                    }else{
                        let i=0;
                        console.log(data.data.data);
                        for(let key of data.data.data){
                            sortUlFour.innerHTML+=`
                            <li class="sortFourLi" _id="${i}" name="${key.brandId}" littleId="${littleId}" flags="3" onclick="sortFourFun()">${key.brandName}</li>
                            `
                            i++
                        }
                       
                    }
                }
                resolve(data.data.data);
            })
            .catch(err => {
                console.log(err);
            })
    });
}
let preChoosed = cl("preChoosed");
let addSortBtn = cl("addSortBtn");
let changeSortBtn = cl("changeSortBtn");
let delSortBtn = cl("delSortBtn");
let oneid=-1,twoid=-1,threeid=-1,fourid=-1;
// 点击一级分类的操作
let oneLi=0;
async function sortOneFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    let sortOneLi = document.getElementsByClassName("sortOneLi");
    sortOneLi[oneLi].style.color="black";
    sortOneLi[oneLi].style.backgroundColor="#fff";
    let bigid = target.getAttribute("name");
    oneid=bigid;
    oneLi = target.getAttribute("_id");
    target.style.color="#438Eb9";
    target.style.backgroundColor="rgb(238, 234, 234)"
    let two = await getSortTwo(bigid);
    let sortUlThree = cl("sortUlThree");
    sortUlThree.innerHTML = "（请选择上一级分类）"
    
    preChoosed.innerHTML =`
        当前选中：${target.innerHTML}（一级分类）
    `
    addSortBtn.setAttribute("flag","1");
    changeSortBtn.setAttribute("flag","1");
    delSortBtn.setAttribute("flag","1");
}
// 点击二级菜单的操作
let twoLi=0;
async function sortTwoFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    let sortTwoLi = document.getElementsByClassName("sortTwoLi");
    sortTwoLi[twoLi].style.color="black";
    sortTwoLi[twoLi].style.backgroundColor="#fff";
    let mediumId = target.getAttribute("name");
    twoLi = target.getAttribute("_id");
    twoid = mediumId;
    target.style.color="#438Eb9";
    target.style.backgroundColor="rgb(238, 234, 234)"

    let three = await getSortThree(mediumId);
    console.log(three);
    let sortUlThree = cl("sortUlThree");

    preChoosed.innerHTML =`
        当前选中：${target.innerHTML}（二级分类）
    `
    addSortBtn.setAttribute("flag","2");
    changeSortBtn.setAttribute("flag","2");
    delSortBtn.setAttribute("flag","2");
}
// 点击三级菜单的操作
let threeLi=0;
async function sortThreeFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    let sortThreeLi = document.getElementsByClassName("sortThreeLi");
    sortThreeLi[threeLi].style.color="black";
    sortThreeLi[threeLi].style.backgroundColor="#fff";
    threeLi = target.getAttribute("_id");
    target.style.color="#438Eb9";
    target.style.backgroundColor="rgb(238, 234, 234)"
    let sortUlThree = cl("sortUlThree");
    
    threeid = target.getAttribute("name");
    let four = await getSortFour(threeid);
    console.log(four);

    preChoosed.innerHTML =`
    当前选中：${target.innerHTML}（三级分类)
    `
    addSortBtn.setAttribute("flag","3");
    changeSortBtn.setAttribute("flag","3");
    delSortBtn.setAttribute("flag","3");
}
// 点击四级菜单的操作
let fourLi=0;
async function sortFourFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    let sortFourLi = document.getElementsByClassName("sortFourLi");
    sortFourLi[fourLi].style.color="black";
    sortFourLi[fourLi].style.backgroundColor="#fff";
    fourLi = target.getAttribute("_id");
    target.style.color="#438Eb9";
    target.style.backgroundColor="rgb(238, 234, 234)"
    fourid = target.getAttribute("name");
    let sortUlThree = cl("sortUlThree");
    
    preChoosed.innerHTML =`
        当前选中：${target.innerHTML}（四级分类)
    `
    addSortBtn.setAttribute("flag","4");
    changeSortBtn.setAttribute("flag","4");
    delSortBtn.setAttribute("flag","4");
}

// 新增子类
function openAddSortFun(e){
    closeSortFun()
    e=e || window.event;
    var target=e.target || e.srcElement;
    let flag = target.getAttribute("flag");
    let addSortChild = cl("addSortChild");
    let addChildInput = cl("addChildInput");
   
    if(flag==1){
        addSortChild.style.display="block";
        addChildInput.setAttribute("flag",1)
        explain({
            code:1,
            msg:"添加二级类",
            time:3000
        })
    }else if(flag==2){
        addSortChild.style.display="block";
        addChildInput.setAttribute("flag",2)
        explain({
            code:1,
            msg:"新增三级",
            time:3000
        })
    }else if(flag==3){
        addSortChild.style.display="block";
        addChildInput.setAttribute("flag",3 )
        explain({
            code:1,
            msg:"新增四级（品牌）",
            time:3000
        })
        
    }else if(flag==4){
        addSortChild.style.display="block";
        addChildInput.setAttribute("flag",4)
        explain({
            code:1,
            msg:"四级分类不可添加子类",
            time:3000
        })
    }else{
        explain({
            code:1,
            msg:"请选择给那个类添加子类",
            time:3000
        })

    }
}
function addSortChildFun(){
    let addChildInput = cl("addChildInput");
    let flag = addChildInput.getAttribute("flag")
    console.log(flag);
    console.log(addChildInput.value);
    if(flag == 1){
         console.log(oneid);
          axios({
            url:'/admin/goods/addSecond',
            method:'POST',
            params: {
                bigId:oneid,
                mType:addChildInput.value
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"添加成功",
                        time:3000
                    })
                    getSortTwo(oneid)
                }else{
                    explain({
                        code:2,
                        msg:"添加失败，请检查是否重复添加相同分类",
                        time:3000
                    })
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==2){
        console.log(twoid);
        axios({
            url:'/admin/goods/addThird',
            method:'POST',
            params: {
                mediumId:twoid,
                lType:addChildInput.value
                }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"添加成功",
                        time:3000
                    })
                    getSortThree(twoid)
                }else{
                    explain({
                        code:2,
                        msg:"添加失败，请检查是否重复添加相同分类",
                        time:3000
                    })
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==3){
        console.log(threeid);
            axios({
                url:'/admin/goods/addFour',
                method:'POST',
                params: {
                    littleId:threeid,
                    brandName:addChildInput.value
                    }
                })
                .then(data => {
                    console.log(12123);
                    if(data.data.code==20000){
                        explain({
                            code:1,
                            msg:"添加成功",
                            time:3000
                        })
                        getSortFour(threeid)
                    }else{
                        explain({
                            code:2,
                            msg:"添加失败，请检查是否重复添加相同分类",
                            time:3000
                        })
                    }
                })
                .catch(err => {
                    console.log(err);
                })
            console.log("1231234156464");
    }    
}
// 开始添加一级类敞口
function openAddOneSortFun(){
    // closeSortFun();
    let addSort = cl("addSort");
    addSort.style.display="block"
    explain({
        code:1,
        msg:"开始添加一级类",
        time:3000
    })
}
function addOneSortFun(){
    let addSortinput = cl("addSortinput");
    console.log(addSortinput.value);
    axios({
        url:'/admin/goods/addFirst',
        method:'POST',
        params: {
            bType:addSortinput.value
        }
        })
        .then(data => {
            if(data.data.code==20000){
                explain({
                    code:1,
                    msg:"添加成功",
                    time:3000
                })
                fun()
            }
        })
        .catch(err => {
            console.log(err);
        })
}
// 修改类别名称
function openChangeSortFun(e){
    closeSortFun()
    e=e || window.event;
    var target=e.target || e.srcElement;
    let flag = target.getAttribute("flag");
    let changeSort = cl("changeSort");
    let changeSortInput = cl("changeSortInput");
    if(flag==1){
        changeSort.style.display="block";
        changeSortInput.setAttribute("flag",1)
        explain({
            code:1,
            msg:"修改一级类",
            time:3000
        })
    }else if(flag==2){
        changeSort.style.display="block";
        changeSortInput.setAttribute("flag",2)
        explain({
            code:1,
            msg:"修改二级类",
            time:3000
        })
    }else if(flag==3){
        changeSort.style.display="block";
        changeSortInput.setAttribute("flag",3)
        explain({
            code:1,
            msg:"修改三级类",
            time:3000
        })
        
    }else if(flag==4){
        changeSort.style.display="block";
        changeSortInput.setAttribute("flag",4)
        explain({
            code:1,
            msg:"修改四级类",
            time:3000
        })
    }else{
        explain({
            code:1,
            msg:"请选择需要修改的类别",
            time:3000
        })

    }
}
function changeSortFun(){
    let changeSortInput = cl("changeSortInput");
    console.log(changeSortInput.value);
    let flag = changeSortInput.getAttribute("flag")
    if(flag==1){
        console.warn(oneid);
        axios({
            url:'/admin/goods/updateFirst',
            method:'PUT',
            params: {
                bigId:oneid,
                bType:changeSortInput.value
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"修改成功",
                        time:3000
                    })
                    fun()
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==2){
        console.warn(twoid);
        axios({
            url:'/admin/goods/updateSecond',
            method:'PUT',
            params: {
                mediumId:twoid,
                mType :changeSortInput.value
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"修改成功",
                        time:3000
                    })
                    getSortTwo(oneid);
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==3){
        console.warn(threeid);
        axios({
            url:'/admin/goods/updateThird',
            method:'PUT',
            params: {
                littleId:threeid,
                lType :changeSortInput.value
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"修改成功",
                        time:3000
                    })
                    getSortThree(twoid)
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==4){
        console.warn(fourid);
        axios({
            url:'/admin/goods/updateFour',
            method:'PUT',
            params: {
                brandId:fourid,
                brandName :changeSortInput.value
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"修改成功",
                        time:3000
                    })
                    getSortFour(fourid)
                }
            })
            .catch(err => {
                console.log(err);
            })
    }
}
// 删除分类
function openDelSortFun(e){
    closeSortFun()
    e=e || window.event;
    var target=e.target || e.srcElement;
    let flag = target.getAttribute("flag");
    let delSort = cl("delSort");
    if(flag==1){
        delSort.style.display="block";
        delSort.setAttribute("flag",1)
        fourLi=0;
        explain({
            code:1,
            msg:"删除一级类",
            time:3000
        })
    }else if(flag==2){
        delSort.style.display="block";
        delSort.setAttribute("flag",2)
        fourLi=0;
        explain({
            code:1,
            msg:"删除二级类",
            time:3000
        })
    }else if(flag==3){
        delSort.style.display="block";
        delSort.setAttribute("flag",3)
        fourLi=0;
        explain({
            code:1,
            msg:"删除三级类",
            time:3000
        })
        
    }else if(flag==4){
        delSort.style.display="block";
        delSort.setAttribute("flag",4)
        fourLi=0;
        explain({
            code:1,
            msg:"删除四级类",
            time:3000
        })
        
    }else{
        explain({
            code:1,
            msg:"请选择需要删除的类别",
            time:3000
        })

    }
}
function delSortFun(){
    let delSort = cl("delSort");
    let flag = delSort.getAttribute("flag");
    if(flag==1){
        console.warn(oneid);
        axios({
            url:'/admin/goods/deleteFirst',
            method:'DELETE',
            params: {
                bigId:oneid,
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"添加成功",
                        time:3000
                    })
                    fun()
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==2){
        console.warn(twoid);
        axios({
            url:'/admin/goods/deleteSecond',
            method:'DELETE',
            params: {
                mediumId:twoid,
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"添加成功",
                        time:3000
                    })
                    getSortTwo(oneid)
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==3){
        console.warn(threeid);
        axios({
            url:'/admin/goods/deleteThird',
            method:'DELETE',
            params: {
                littleId:threeid,
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"添加成功",
                        time:3000
                    })
                    getSortThree(twoid)
                }else{
                    explain({
                        code:2,
                        msg:"删除失败",
                        time:3000
                    })
                }
            })
            .catch(err => {
                console.log(err);
            })
    }else if(flag==4){
        console.warn(fourid);
        axios({
            url:'/admin/goods/deleteFour',
            method:'DELETE',
            params: {
                brandId :fourid,
            }
            })
            .then(data => {
                if(data.data.code==20000){
                    explain({
                        code:1,
                        msg:"添加成功",
                        time:3000
                    })
                    getSortFour(threeid)
                }else{
                    explain({
                        code:2,
                        msg:"删除失败",
                        time:3000
                    })
                }
            })
            .catch(err => {
                console.log(err);
            })
    }
}

// 关闭函数
function closeSortFun(){
    let changeSort = cl("changeSort");
    let addSortChild = cl("addSortChild");
    let addSort = cl("addSort");
    let delSort = cl("delSort");
    changeSort.style.display="none"
    addSortChild.style.display="none"
    addSort.style.display="none"
    delSort.style.display="none"
}

